﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MDSAdministration
{
    /// <summary>
    /// Interaction logic for SortOrder.xaml
    /// </summary>
    public partial class SortOrder : Window
    {
        private LinearGradientBrush lgbLightGreenVerticalGradient;

        public CompareEntity Entity { get; set; }
        public ServerMDSDetails Server { get; set; }

        public bool Deployed { get; set; }

        private SortDescription sdChangeSortOrder = new SortDescription("ChangeSortOrder", ListSortDirection.Ascending);
        private int MaxSystemAttribute = 0;
        private ListCollectionView collection;

        public SortOrder()
        {
            InitializeComponent();

            lgbLightGreenVerticalGradient = new LinearGradientBrush();
            lgbLightGreenVerticalGradient.StartPoint = new Point(0.5, 0);
            lgbLightGreenVerticalGradient.EndPoint = new Point(0.5, 1);
            lgbLightGreenVerticalGradient.GradientStops.Add(new GradientStop(Colors.LightGreen, 0.0));
            lgbLightGreenVerticalGradient.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lblEntityName.Content = "Entity:" + Entity.Name;

            lblServer.Content = "SERVER: " + Server.ServerName +
                                " DATABASE: " + Server.Database +
                                " MODEL: " + Server.Model +
                                " VERSION: " + Server.ModelVersion;

            copySortOrderToChange();
            getMaxSystemAttributeSortOrder();


            collection = new ListCollectionView(Entity.Attributes.Values.ToList());
            collection.SortDescriptions.Add(sdChangeSortOrder);

            dgSortOrder.DataContext = collection;
        }

        private void getMaxSystemAttributeSortOrder()
        {
            MaxSystemAttribute = (from a in Entity.Attributes.Values where a.IsSystem == true select a.SortOrder).Max();
        }

        private void copySortOrderToChange()
        {
            foreach (var a in Entity.Attributes.Values)
            {
                a.ChangeSortOrder = a.SortOrder;
            }
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            bool update = false;
            if (dgSortOrder.SelectedItem != null)
            {
                CompareAttribute ca = (CompareAttribute)dgSortOrder.SelectedItem;

                var caPlus = (from a in Entity.Attributes.Values 
                              where a.ChangeSortOrder <= (ca.ChangeSortOrder - 1)
                              && a.IsSystem == false
                              orderby a.ChangeSortOrder descending 
                              select a).FirstOrDefault();

                if (caPlus != null)
                {
                    caPlus.ChangeSortOrder = ca.ChangeSortOrder;
                    ca.ChangeSortOrder -= 1;
                    update = true;
                }
            }

            refreshGrid(update);
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            bool update = false;
            if (dgSortOrder.SelectedItem != null)
            {
                CompareAttribute ca = (CompareAttribute) dgSortOrder.SelectedItem;

                var caMinus = (from a in Entity.Attributes.Values
                               where a.ChangeSortOrder >= (ca.ChangeSortOrder + 1)
                               && a.IsSystem == false
                               orderby a.ChangeSortOrder ascending 
                               select a).FirstOrDefault();
                
                if(caMinus != null)
                {
                    caMinus.ChangeSortOrder = ca.ChangeSortOrder;
                    ca.ChangeSortOrder += 1;
                    update = true;
                }
            }

            refreshGrid(update);
        }

        private void refreshGrid(bool update)
        {
            if (update)
            {
                collection.SortDescriptions.Remove(sdChangeSortOrder);
                collection.SortDescriptions.Add(sdChangeSortOrder);

                dgSortOrder.Items.Refresh();
            }
            dgSortOrder.Focus();
        }

        private void btnResequence_Click(object sender, RoutedEventArgs e)
        {
            int index = MaxSystemAttribute + 1;

            collection.MoveCurrentToFirst();

            do
            {
                CompareAttribute ca = (CompareAttribute)collection.CurrentItem;
               
                if (ca.IsSystem == false)
                {
                    ca.ChangeSortOrder = index;
                    index++;
                }

            } while (collection.MoveCurrentToNext());

            refreshGrid(true);
        }

        private void dgSortOrder_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            CompareAttribute RowDataContaxt = e.Row.DataContext as CompareAttribute;
            if (RowDataContaxt != null)
            {
                if (RowDataContaxt.IsSystem)
                {
                    e.Row.Background = lgbLightGreenVerticalGradient;
                }
            }
        }

        private void btnDeploy_Click(object sender, RoutedEventArgs e)
        {
            DeploymentBuilder db = new DeploymentBuilder();
            db.CurrentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            db.Server = Server;

            foreach (var ca in Entity.Attributes.Values)
            {
                if (ca.SortOrder != ca.ChangeSortOrder)
                {
                    db.LActionAttributes.Add(new ActionAttribute()
                                                 {
                                                     ca = ca,
                                                     status = ObjectStatus.SortOrder
                                                 });
                }
            }
            

            var dlg = new DeploymentWindow();
            dlg.db = db;
            dlg.ShowDialog();

            Deployed = dlg.Deployed;
        }
    }

}
